var detailSize = 0;
var materialDetailData = [];
var materialDetailText = "";
init();

function init() {
    queryProjectList(0);
    initMaterialFirstType();
}

/**
 * 一级材料类型加载
 */
function initMaterialFirstType(){
    $.ajax({
        url :   prefixUrl + "material/type/queryList",
        type:   "post",
        data:JSON.stringify({}),
        contentType:"application/json;",
        success:function(data) {
            console.log(data);
            var firstMaterialHtml = "";
            var list = data.data;
            for(var i=0; i<list.length; i++){
                firstMaterialHtml = firstMaterialHtml
                    +'<option style="text-align: center;" value="'
                    +list[i].typeCode
                    +'">'
                    +list[i].typeName
                    +'</option>';
            }
            $("#firstMaterial").html(firstMaterialHtml);
            if(list != null && list.length > 0){
                initMaterialTwoType(list[0].typeCode);
            }
        }
    });
}

/**
 * 一级材料类型监听事件
 */
$("#firstMaterial").change(function(){
    var typeCode = $("#firstMaterial").val();
    initMaterialTwoType(typeCode);
});

/**
 * 二级材料类型加载
 */
function initMaterialTwoType(typeCode){
    $.ajax({
        url :   prefixUrl + "material/type/queryList",
        type:   "post",
        data:JSON.stringify({typeCode:typeCode}),
        contentType:"application/json;",
        success:function(data) {
            console.log(data);
            var twoMaterialHtml = "";
            var list = data.data;
            for(var i=0; i<list.length; i++){
                twoMaterialHtml = twoMaterialHtml
                    +'<option style="text-align: center;" value="'
                    +list[i].typeId
                    +'">'
                    +list[i].typeName
                    +'</option>';
            }
            $("#twoMaterial").html(twoMaterialHtml);
            //initMaterial("materialName" + detailSize,$("#twoMaterial").val());
        }
    });
}

function queryProjectList(pageIndex) {
    $.ajax({
        url: prefixUrl + "project/queryList",
        type: "post",
        data: JSON.stringify({pageIndex:pageIndex}),
        contentType: "application/json;",
        success: function (data) {
            var projectHtml = "";
            var list = data.data.rows;
            for(var i=0; i<list.length; i++){
                projectHtml = projectHtml
                    +'<option style="text-align: center;" value="'
                    +list[i].projectId
                    +'">'
                    +list[i].projectName
                    +'</option>';
            }
            $("#projectList").html(projectHtml);
        }
    })
}

function addMaterial() {
    var html = html
        +'<tr>'
        +'<th>'
        +'<div class="form-group">'
        +'<div class="col-sm-8">'
        +'<select id="materialName'
        + detailSize
        + '"  name="materialName" class="ui-select mar-r-5">'
        +'</select>'
        +'</div>'
        +'</div>'
        +'</th>'
        +'<th>'
        +'<div class="form-group">'
        +'<div class="col-sm-8">'
        +'<input id="materialNumber'
        + detailSize
        + '" name="materialNumber" type="text" class="form-control1" placeholder="请在此输入材料数量与单位" maxlength="20">'
        +'</div>'
        +'</div>'
        +'</th>'
        +'</tr>';
    if($("#materialNumber" + (detailSize-1)).val() == ""){
        alert("请先填写数量，再进行增加下一分类");
        return;
    }
    initMaterial('materialName'+ detailSize + '',$("#twoMaterial").val());
    $("#materialDetail").append(html);
    detailSize = detailSize + 1;
}

function initMaterial(materialId,materialTypeId){
    $.ajax({
        url :   prefixUrl + "material/queryList",
        type:   "post",
        data:JSON.stringify({materialTypeId:materialTypeId}),
        contentType:"application/json;",
        success:function(data) {
            var materialHtml = "";
            var list = data.data.rows;
            for(var i=0; i<list.length; i++){
                materialHtml = materialHtml
                    +'<option style="text-align: center;" value="'
                    +list[i].materialId
                    +'">'
                    +list[i].materialName
                    +'</option>';
            }
            $("#" + ''+ materialId +'' + "").html(materialHtml);
            return true;
            /*if(list.length != 0){
                for(var i=0; i<list.length; i++){
                    materialHtml = materialHtml
                        +'<option style="text-align: center;" value="'
                        +list[i].materialId
                        +'">'
                        +list[i].materialName
                        +'</option>';
                }
                $("#" + ''+ materialId +'' + "").html(materialHtml);
                return true;
            }else{
                initMaterial(materialId,null);
            }*/
        }
    });

}

/**
 * 设置明细信息
 */
function saveDetail() {

    if($("#materialNumber" + (detailSize - 1)).val() == ""){
        alert("请先填写数量");
        return;
    }

    materialDetailText = "";
    materialDetailData = [];

    var materialList = document.getElementsByName("materialName");
    for(var i=0; i<materialList.length; i++){
        //1.增加界面回显
        materialDetailText = materialDetailText + $("#" + 'materialName' + i + '' +" option:selected").text() + "(" + $("#" + 'materialNumber' + i + '' +"").val() + "),";

        //2.增加接口明细数据
        materialDetailData[i] = {
            materialId : $("#" + 'materialName' + i + '' +" option:selected").val(),
            materialName : $("#" + 'materialName' + i + '' +" option:selected").text(),
            materialNumber : $("#" + 'materialNumber' + i + '' +"").val()
        };

        //3.界面赋值
        $("#materialDetailList").val(materialDetailText);

        modalDisplay();
    }
}

/**
 * 增加记录
 */
function saveMaterial() {

    if($("#projectList").val() == null){
        alert("项目不可为空");
        return;
    }
    if($("#applyName").val() == null||$("#applyName").val()==""){
        alert("申请人姓名不能为空");
        return;
    }
    if($("input[type='radio']:checked").val() == null){
        alert("状态不可为空");
        return;
    }
    if(materialDetailData.length == 0){
        alert("材料明细不可为空");
        return;
    }
    var data = {

        applyName:$("#applyName").val(),
        projectId : $("#projectList").val(),
        projectName : $("#projectList").find("option:selected").text(),
        state : $("input[type='radio']:checked").val(),
        materialDesc : $("#materialDesc").val(),
        list : materialDetailData
    };

    $.ajax({
        url :   prefixUrl + "warehouse/addwarehouse",
        type:   "post",
        data:JSON.stringify(data),
        contentType:"application/json;",
        success:function(data) {
            alert(data.msg);
            window.location.reload();
        }
    });
}